package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.DrmoPackageDao;
import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.model.DrmoPackage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Hibernate DAO for DrmoPackageDispatchAction objects
 *
 * @author TSgt Jason Ferguson
 */
@Repository("drmoPackageDao")
public class HibernateDrmoPackageDao extends HibernateGenericDao<DrmoPackage> implements DrmoPackageDao {

    private static final Log log = LogFactory.getLog(HibernateDrmoPackageDao.class);

    public HibernateDrmoPackageDao() {
        super(DrmoPackage.class);
    }

    public DrmoPackage getByDtid(String dtid) {

        Criteria drmoCriteria = sessionFactory.getCurrentSession().createCriteria(DrmoPackage.class);
        drmoCriteria.add(Restrictions.ilike("dtid", dtid));

        DrmoPackage result = null;
        try {
            result = (DrmoPackage) drmoCriteria.uniqueResult();
        } catch (HibernateException e) {
            log.debug("getByDtid() threw HibernateException", e);
        }

        return result;
//        DetachedCriteria crit = DetachedCriteria.forClass(DrmoPackage.class);
//        crit.add(Restrictions.ilike("dtid", dtid));
//
//        List<DrmoPackage> results = (List<DrmoPackage>) getHibernateTemplate().findByCriteria(crit);
//
//        if (results.size() > 0) {
//            return results.get(0);
//        } else {
//            return null;
//        }
    }

    public List<DrmoPackage> getAllByAdpeAccount(AdpeAccount acct) {

        Criteria drmoCriteria = sessionFactory.getCurrentSession().createCriteria(DrmoPackage.class);
        drmoCriteria.createCriteria("adpeAccount");
        drmoCriteria.add(Restrictions.eq("id", acct.getId()));

        return (List<DrmoPackage>) drmoCriteria.list();

//        DetachedCriteria crit = DetachedCriteria.forClass(DrmoPackage.class);
//        DetachedCriteria adpeCrit = crit.createCriteria("adpeAccount");
//        adpeCrit.add(Restrictions.eq("id", acct.getId()));
//
//        return (List<DrmoPackage>) getHibernateTemplate().findByCriteria(crit);
    }

    public List<DrmoPackage> getPendingByAdpeAccount(AdpeAccount acct) {

        Criteria drmoCriteria = sessionFactory.getCurrentSession().createCriteria(DrmoPackage.class);
        Criteria adpeAcctCriteria = drmoCriteria.createCriteria("adpeAccount");
        adpeAcctCriteria.add(Restrictions.eq("id", acct.getId()));
        drmoCriteria.add(Restrictions.isNull("turninDate"));

        return (List<DrmoPackage>) drmoCriteria.list();
//        DetachedCriteria crit = DetachedCriteria.forClass(DrmoPackage.class);
//        DetachedCriteria adpeCrit = crit.createCriteria("adpeAccount");
//        adpeCrit.add(Restrictions.eq("id", acct.getId()));
//
//        crit.add(Restrictions.isNull("turninDate"));

//        return (List<DrmoPackage>) getHibernateTemplate().findByCriteria(crit);
    }

    public int getPendingCountByAdpeAccount(AdpeAccount acct) {

        Criteria drmoCriteria = sessionFactory.getCurrentSession().createCriteria(DrmoPackage.class);
        Criteria adpeAcctCriteria = drmoCriteria.createCriteria("adpeAccount");
        adpeAcctCriteria.add(Restrictions.eq("id", acct.getId()));
        drmoCriteria.setProjection(Projections.rowCount());

        return (Integer) drmoCriteria.uniqueResult();

//        DetachedCriteria crit = DetachedCriteria.forClass(DrmoPackage.class);
//        DetachedCriteria adpeCrit = crit.createCriteria("adpeAccount");
//        adpeCrit.add(Restrictions.eq("id", acct.getId()));
//
//        crit.setProjection(Projections.rowCount());
//
//        return (Integer) getHibernateTemplate().findByCriteria(crit).get(0);
    }
}
